package apple.encryptedNote.tools;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
import apple.encryptedNode.Constans;

/**
 * 发送短信
 * 
 * @param <CryptoSMS>
 * **/
public class SMSService {
	private Context context;
	private final static String TAG = SMSService.class.getSimpleName();

	public SMSService(Context context) {
		this.context = context;
	}

	/**
	 * 发送文字短信
	 * ***/
	public int send(String phonenum, String content) {
		int ret = 0;
		if ((phonenum == null || "".equals(phonenum))
				|| (content == null || "".equals(content))) {

			Toast.makeText(context, "电话号码，短信内容不能为空!", Toast.LENGTH_LONG);
			ret = 1;
		} else {
			SmsManager manage = SmsManager.getDefault();
			List<String> all = manage.divideMessage(content);
			Iterator<String> it = all.iterator();
			while (it.hasNext()) {
				manage.sendTextMessage(phonenum, null, it.next(), null, null); // 逐条发送短息

			}
			Toast.makeText(context, "发送成功!", Toast.LENGTH_LONG);
		}
		return ret;

	}

	/**
	 * 发送字节短信
	 * ***/
	public int send(String phonenum, byte result[]) {
		Log.d(TAG, "send");
		int ret = 0;
		if ((phonenum == null || "".equals(phonenum))
				|| (result == null || "".equals(result))) {

			Toast.makeText(context, "电话号码，短信内容不能为空!", Toast.LENGTH_LONG);
			Log.d(TAG, "电话号码，短信内容不能为空!");
			ret = 1;
		} else {
			SmsManager manage = SmsManager.getDefault();

			short pt = 80; /* 设置端口号 */
			PendingIntent pi = PendingIntent.getBroadcast(context, 0,
					new Intent(), 0);
			manage.sendDataMessage(phonenum, null, pt, result, pi, null); // 填写接收

			Toast.makeText(context, "发送成功!", Toast.LENGTH_LONG);
		}
		return ret;

	}

	/**
	 * 
	 * 批量发送发送加密短信
	 * 
	 * ***/
	public void sendSms2(List<String> selectedIds, String value) {
		Log.d(TAG, "sendSms2 发送加密短信");
		try {
			Log.d(TAG, "加密前:" + value);
			value = DesUtils.encode(Constans.key, value);

			Log.d(TAG, "加密后:" + value);
			if (selectedIds.size() > 0)//
			{
				for (int j = 0; j < selectedIds.size(); j++) {
					String phone = selectedIds.get(j).substring(0, 11);// 得到前11位，去掉后3位随机数
					Log.d(TAG, "phone:" + phone);

					send(phone, value);

				}

			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			Toast.makeText(context, "加密失败", Toast.LENGTH_LONG).show();
		}

	}

	/****
	 * 解密短信
	 * 
	 * **/
	public List<String> jiemi3(Object[] pdus) {
		Log.d(TAG, "jiemi3 解密短信");
		String phone = "";
		String jiemi = "";
		List<String> ret = new ArrayList<String>();
		SmsMessage[] messages = new SmsMessage[pdus.length];
		for (int i = 0; i < pdus.length; i++) {
			messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
		}
		for (SmsMessage message : messages) {
			phone = message.getDisplayOriginatingAddress();
			String neirong = message.getDisplayMessageBody();
			Log.d(TAG, "号码:" + phone);
			Log.d(TAG, "内容:" + neirong);
			Toast.makeText(context,
					"DecryptoSMS->phone:" + phone + ",内容:" + neirong,
					Toast.LENGTH_LONG).show();
			Log.d(TAG, "解密==============================》");

			jiemi = DesUtils.decode(Constans.key, neirong);

			Log.d(TAG, "解密结果：" + jiemi);
			Toast.makeText(context, "解密结果：" + jiemi, Toast.LENGTH_LONG).show();
			ret.add(phone);
			ret.add(jiemi);

		}
		return ret;
	}
}
